Skip to content

Restore matplotlib backend after HoloViews matplotlib plot#1538

Merged
rajeeja merged 10 commits into
mainfrom
rajeeja/fix_matplotlib_backend
Jul 14, 2026
Merged

Restore matplotlib backend after HoloViews matplotlib plot#1538
rajeeja merged 10 commits into
mainfrom
rajeeja/fix_matplotlib_backend

Conversation

@rajeeja

@rajeeja rajeeja commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Calling plot(backend="matplotlib") runs hv.extension("matplotlib"), which switches the active matplotlib backend and clobbers the IPython inline display hook, silently breaking any subsequent native matplotlib/xarray .plot() calls. This restores the original matplotlib backend right after the HoloViews extension switch, which is safe because HoloViews objects display through Store.current_backend rather than the active matplotlib backend. Verified in real Jupyter kernels that the reported sequence now works, with a new regression test and all plotting tests/relevant docs notebooks passing; closes #1537.

plot(backend='matplotlib') calls hv.extension('matplotlib'), which switches
the active matplotlib backend and clobbers the IPython inline display hook,
silently breaking subsequent native matplotlib/xarray .plot() calls. Restore
the original matplotlib backend right after the HoloViews extension switch;
HoloViews objects still display via Store.current_backend, so this is safe.

Closes #1537

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a Jupyter/IPython plotting regression where uxarray.plot(..., backend="matplotlib") triggers hv.extension("matplotlib"), which can alter Matplotlib’s active backend and break subsequent native matplotlib/xarray plotting in the same session.

Changes:

  • Restore the Matplotlib backend immediately after switching HoloViews to the matplotlib backend.
  • Update reset_mpl_backend() documentation to describe intended behavior.
  • Add a regression test covering backend restoration after a UXarray matplotlib-backed plot.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
uxarray/plot/utils.py Adds post-hv.extension("matplotlib") backend restoration and updates backend reset docstring.
test/test_plot.py Adds a regression test asserting Matplotlib backend state and subsequent xarray plotting still works.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread uxarray/plot/utils.py
Comment thread uxarray/plot/utils.py
Comment thread test/test_plot.py Outdated
@cmdupuis3 cmdupuis3 added the run-benchmark Run ASV benchmark workflow label Jul 2, 2026
@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown

ASV Benchmarking

Benchmark Comparison Results

Benchmarks that have improved:

Change Before [f578e30] After [a1814cc] Ratio Benchmark (Parameter)
- 577M 391M 0.68 face_bounds.FaceBounds.peakmem_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/geoflow-small/grid.nc'))
- 694M 390M 0.56 face_bounds.FaceBounds.peakmem_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/quad-hexagon/grid.nc'))
- 497M 384M 0.77 mpas_ocean.Gradient.peakmem_gradient('480km')

Benchmarks that have stayed the same:

Change Before [f578e30] After [a1814cc] Ratio Benchmark (Parameter)
7.69±0.06μs 7.81±0.1μs 1.02 bench_connectivity.Connectivity.time_edge_face('120km')
7.86±0.1μs 8.00±0.2μs 1.02 bench_connectivity.Connectivity.time_edge_face('480km')
8.05±0.09μs 7.88±0.1μs 0.98 bench_connectivity.Connectivity.time_edge_node('120km')
8.10±0.08μs 8.08±0.1μs 1 bench_connectivity.Connectivity.time_edge_node('480km')
7.89±0.2μs 7.80±0.1μs 0.99 bench_connectivity.Connectivity.time_face_edge('120km')
8.11±0.06μs 8.18±0.1μs 1.01 bench_connectivity.Connectivity.time_face_edge('480km')
8.08±0.06μs 7.70±0.08μs 0.95 bench_connectivity.Connectivity.time_face_face('120km')
8.15±0.08μs 8.02±0.1μs 0.98 bench_connectivity.Connectivity.time_face_face('480km')
15.8±0.08μs 15.8±0.2μs 1 bench_connectivity.Connectivity.time_face_node('120km')
16.3±0.1μs 16.3±0.1μs 1 bench_connectivity.Connectivity.time_face_node('480km')
8.01±0.06μs 7.96±0.03μs 0.99 bench_connectivity.Connectivity.time_node_edge('120km')
7.93±0.3μs 8.16±0.1μs 1.03 bench_connectivity.Connectivity.time_node_edge('480km')
7.97±0.05μs 7.80±0.05μs 0.98 bench_connectivity.Connectivity.time_node_face('120km')
8.33±0.08μs 8.06±0.1μs 0.97 bench_connectivity.Connectivity.time_node_face('480km')
389M 389M 1 face_bounds.FaceBounds.peakmem_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/mpas/QU/oQU480.231010.nc'))
421M 419M 1 face_bounds.FaceBounds.peakmem_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/scrip/outCSne8/outCSne8.nc'))
15.5±0.6ms 15.7±0.2ms 1.01 face_bounds.FaceBounds.time_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/mpas/QU/oQU480.231010.nc'))
3.47±0.03ms 3.49±0.2ms 1 face_bounds.FaceBounds.time_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/scrip/outCSne8/outCSne8.nc'))
20.8±0.2ms 21.3±0.05ms 1.02 face_bounds.FaceBounds.time_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/geoflow-small/grid.nc'))
1.50±0.06ms 1.55±0.04ms 1.04 face_bounds.FaceBounds.time_face_bounds(PosixPath('/home/runner/work/uxarray/uxarray/test/meshfiles/ugrid/quad-hexagon/grid.nc'))
800±10ms 810±6ms 1.01 import.Imports.timeraw_import_uxarray
658±9ns 644±10ns 0.98 mpas_ocean.CheckNorm.time_check_norm('120km')
602±5ns 585±5ns 0.97 mpas_ocean.CheckNorm.time_check_norm('480km')
623±7ms 630±6ms 1.01 mpas_ocean.ConnectivityConstruction.time_face_face_connectivity('120km')
38.3±0.5ms 38.4±0.1ms 1 mpas_ocean.ConnectivityConstruction.time_face_face_connectivity('480km')
636±10μs 640±20μs 1.01 mpas_ocean.ConnectivityConstruction.time_n_nodes_per_face('120km')
480±10μs 499±6μs 1.04 mpas_ocean.ConnectivityConstruction.time_n_nodes_per_face('480km')
4.11±0.04ms 4.06±0.04ms 0.99 mpas_ocean.ConstructFaceLatLon.time_cartesian_averaging('120km')
3.04±0.04ms 3.03±0.04ms 1 mpas_ocean.ConstructFaceLatLon.time_cartesian_averaging('480km')
2.67±0.01s 2.67±0.02s 1 mpas_ocean.ConstructFaceLatLon.time_welzl('120km')
176±2ms 170±0.9ms 0.97 mpas_ocean.ConstructFaceLatLon.time_welzl('480km')
10.6±0.1ms 10.8±0.1ms 1.02 mpas_ocean.ConstructTreeStructures.time_ball_tree('120km')
856±10μs 853±20μs 1 mpas_ocean.ConstructTreeStructures.time_ball_tree('480km')
7.89±0.3ms 8.17±0.02ms 1.04 mpas_ocean.ConstructTreeStructures.time_kd_tree('120km')
687±20μs 681±9μs 0.99 mpas_ocean.ConstructTreeStructures.time_kd_tree('480km')
552±3ms 557±4ms 1.01 mpas_ocean.CrossSections.time_const_lat('120km', 1)
278±4ms 278±4ms 1 mpas_ocean.CrossSections.time_const_lat('120km', 2)
144±2ms 145±2ms 1.01 mpas_ocean.CrossSections.time_const_lat('120km', 4)
419±8ms 414±1ms 0.99 mpas_ocean.CrossSections.time_const_lat('480km', 1)
211±2ms 204±2ms 0.97 mpas_ocean.CrossSections.time_const_lat('480km', 2)
108±0.7ms 106±1ms 0.98 mpas_ocean.CrossSections.time_const_lat('480km', 4)
21.7±0.5ms 21.9±0.2ms 1.01 mpas_ocean.DualMesh.time_dual_mesh_construction('120km')
2.33±0.06ms 2.36±0.05ms 1.02 mpas_ocean.DualMesh.time_dual_mesh_construction('480km')
676±5ms 691±10ms 1.02 mpas_ocean.GeoDataFrame.time_to_geodataframe('120km', False)
42.0±0.9ms 42.9±0.8ms 1.02 mpas_ocean.GeoDataFrame.time_to_geodataframe('120km', True)
60.6±0.8ms 61.2±0.5ms 1.01 mpas_ocean.GeoDataFrame.time_to_geodataframe('480km', False)
4.86±0.04ms 5.00±0.05ms 1.03 mpas_ocean.GeoDataFrame.time_to_geodataframe('480km', True)
404M 404M 1 mpas_ocean.Gradient.peakmem_gradient('120km')
189±3ms 197±2ms 1.04 mpas_ocean.Gradient.time_gradient('120km')
12.7±0.09ms 13.2±0.2ms 1.03 mpas_ocean.Gradient.time_gradient('480km')
241±2μs 242±3μs 1 mpas_ocean.HoleEdgeIndices.time_construct_hole_edge_indices('120km')
88.9±0.9μs 89.1±1μs 1 mpas_ocean.HoleEdgeIndices.time_construct_hole_edge_indices('480km')
351M 354M 1.01 mpas_ocean.Integrate.peakmem_integrate('120km')
331M 331M 1 mpas_ocean.Integrate.peakmem_integrate('480km')
152±2μs 154±2μs 1.01 mpas_ocean.Integrate.time_integrate('120km')
142±7μs 137±7μs 0.97 mpas_ocean.Integrate.time_integrate('480km')
133±3ms 134±1ms 1.01 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('120km', 'exclude')
132±1ms 134±2ms 1.01 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('120km', 'include')
130±2ms 132±5ms 1.01 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('120km', 'split')
10.5±0.4ms 10.4±0.3ms 1 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('480km', 'exclude')
10.6±0.2ms 10.5±0.1ms 1 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('480km', 'include')
10.5±0.1ms 10.7±0.3ms 1.02 mpas_ocean.MatplotlibConversion.time_dataarray_to_polycollection('480km', 'split')
181±5μs 181±8μs 1 mpas_ocean.PointInPolygon.time_face_search_lonlat('120km')
186±2μs 183±5μs 0.98 mpas_ocean.PointInPolygon.time_face_search_lonlat('480km')
173±2μs 174±6μs 1 mpas_ocean.PointInPolygon.time_face_search_xyz('120km')
172±1μs 172±5μs 1 mpas_ocean.PointInPolygon.time_face_search_xyz('480km')
161±2ms 158±0.5ms 0.98 mpas_ocean.RemapDownsample.time_bilinear_remapping
160±2ms 158±4ms 0.99 mpas_ocean.RemapDownsample.time_inverse_distance_weighted_remapping
3.28±0.03ms 3.28±0.06ms 1 mpas_ocean.RemapDownsample.time_nearest_neighbor_remapping
870±20ms 845±30ms 0.97 mpas_ocean.RemapUpsample.time_bilinear_remapping
30.9±0.2ms 30.8±1ms 1 mpas_ocean.RemapUpsample.time_inverse_distance_weighted_remapping
25.7±0.4ms 25.3±0.4ms 0.98 mpas_ocean.ZonalAverage.time_zonal_average('120km')
4.95±0.08ms 4.75±0.2ms 0.96 mpas_ocean.ZonalAverage.time_zonal_average('480km')
326M 328M 1.01 quad_hexagon.QuadHexagon.peakmem_open_dataset
326M 326M 1 quad_hexagon.QuadHexagon.peakmem_open_grid
5.63±0.3ms 5.77±0.4ms 1.03 quad_hexagon.QuadHexagon.time_open_dataset
4.77±0.2ms 4.66±0.2ms 0.98 quad_hexagon.QuadHexagon.time_open_grid

Benchmarks that have got worse:

Change Before [f578e30] After [a1814cc] Ratio Benchmark (Parameter)
+ 6.85±0.1ms 8.06±0.2ms 1.18 mpas_ocean.RemapUpsample.time_nearest_neighbor_remapping

@Sevans711

Copy link
Copy Markdown
Collaborator

I was taking a quick look to see if I could review and get this merged to main, but I ended up with a couple quick questions.

(1) Can you provide a small example that definitely causes the unexpected behavior mentioned in #1537 before this fix, and does not cause it anymore after this fix?

On main currently (i.e. without this fix yet), in a Jupyter notebook, I ran the following, putting each plot() call in its own cell. I tried in Python 3.10 and Python 3.13. In both cases, every plot call displays a plot; I can't reproduce the failure to make plots. Each of these calls successfully makes a plot.

import uxarray as ux
uxds = ux.tutorial.open_dataset("outCSne30-vortex")
uxds["psi"].plot(backend='matplotlib')  # makes a matplotlib-style plot
uxds["psi"].plot()  # makes a matplotlib-style plot
# try switching to bokeh just in case it is related to switching back and forth:
uxds["psi"].plot(backend='bokeh')  # makes a bokeh-style plot
uxds["psi"].plot()  # makes a bokeh-style plot
# switch back again:
uxds["psi"].plot(backend='matplotlib')  # makes a matplotlib-style plot
uxds["psi"].plot()  # makes a matplotlib-style plot

(2) Is it desirable for the backend to be changed "permanently" instead of just a temporary change for the current plot? I naively would expect a kwarg being passed to plot() to only affect behavior for that one plot, not to affect global state. E.g., I would expect the following:

import holoviews as hv
hv.extension('bokeh')
uxds["psi"].plot()  # makes a bokeh-style plot
uxds["psi"].plot(backend='matplotlib')  # makes a matplotlib-style plot
uxds["psi"].plot()  # I would expect a bokeh-style plot, but actually it is matplotlib-style.

Please let me know if this second point belongs in a separate issue instead.

@rajeeja

rajeeja commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

Thanks. I’ll keep this separate from #1541. Test added.

@Sevans711

Copy link
Copy Markdown
Collaborator

I agree this should stay separate from #1541. I thought both of my points were unrelated to 1541, though.

For (1), I see you added regression tests, with good comments detailing how they aren't actually producing a real Jupyter notebook to see the issue occur (that would be challenging to set up with pytest). Were you able to reproduce the original issue on a real Jupyter notebook before this fix?

Ideally, I would like to run a small piece of code which can reproduce the issue when in main in a real Jupyter notebook; then change to this branch, rerun the code, and see that the issue does not occur anymore.

@rajeeja rajeeja moved this to 👀 In review in UXarray Development Jul 8, 2026
@Sevans711

Copy link
Copy Markdown
Collaborator

Ah, I misread the original bug. It isn't saying that uxarray.UxDataArray.plot() fails, it's saying that xarray.DataArray.plot() fails. I believe your changes so far have not fixed this bug. From within this branch, I still see the bug:

Screenshot 2026-07-09 at 11 15 13 AM

What I would expect, once the bug has been fixed, would be for the xr.DataArray([1,2,3]).plot() cell to show a plot, like it does if it is run before calling any uxarray plotting routines:

Screenshot 2026-07-09 at 11 11 51 AM

@rajeeja rajeeja requested a review from Sevans711 July 9, 2026 15:52
@rajeeja

rajeeja commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

Yes — I reproduced this in notebook-style execution.

Minimal check:

%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
import uxarray as ux

uxds = ux.tutorial.open_dataset("outCSne30-vortex")
print("initial:", mpl.get_backend())
uxds["psi"].plot(backend="matplotlib")
print("after uxarray:", mpl.get_backend())

plt.figure()
plt.plot([0, 1], [0, 1])
plt.show()

On main, the UXarray plot changes Matplotlib from inline to agg, and the later native Matplotlib plot warns that FigureCanvasAgg is non-interactive.

On this branch, the backend is restored to inline, and the later native Matplotlib plot displays normally.

@Sevans711

Copy link
Copy Markdown
Collaborator

Ah, I can confirm, if I explicitly include %matplotlib inline then things work on this branch and not on main.

However, when I don't include it, even on this branch, the results are that no plot gets shown:
Screenshot 2026-07-09 at 12 54 42 PM

I basically never explicitly write out %matplotlib inline in my notebooks. Can we make a fix that works even when you don't include that line?

rajeeja added 2 commits July 9, 2026 17:17
mpl.use() restores the matplotlib backend name but does not re-register
IPython's inline display integration that hv.extension('matplotlib')
clobbers. In a Jupyter kernel without an explicit %matplotlib inline,
native matplotlib/xarray .plot() calls after a uxarray matplotlib plot
still failed to render. Re-run configure_inline_support when the restored
backend is inline so the display hook is reinstated. See #1537.
@Sevans711

Copy link
Copy Markdown
Collaborator

Thank you for working through all these unexpected complications…. I was reviewing and ready to approve, but then somehow discovered another failure case.

First of all, your fix looks good for what we observed already. I confirmed on my end that the following runs and creates a plot, as expected:

# intentionally skipping: %matplotlib inline
import matplotlib.pyplot as plt
import uxarray as ux

uxds = ux.tutorial.open_dataset("outCSne30-vortex")
uxds["psi"].plot(backend="matplotlib")

plt.figure()
plt.plot([0, 1], [0, 1])
plt.show()

Now, for the failure case: it is about what happens when you don't call plt.show() explicitly. When I remove it, I don't see any plot:

import matplotlib.pyplot as plt
import uxarray as ux

uxds = ux.tutorial.open_dataset("outCSne30-vortex")
uxds["psi"].plot(backend="matplotlib")

plt.figure()  # this line isn't actually necessary but it doesn't hurt.
plt.plot([0, 1], [0, 1])

This leads to not displaying any figure.

The issue also persists across all subsequent matplotlib plots in the notebook as well, for example doing plt.plot([1,2,3,2,1]) in another cell will lead to no plot being displayed. Calling plt.show() at a later time will show all plots that have been created since the previous time plt.show() was called.

That's not ideal; normally I expect Jupyter to display plots if they are the last line in the cell. E.g., in a fresh kernel, if I run the following:

import matplotlib.pyplot as plt
plt.plot([0,1],[0,1])

Then a plot gets displayed. I usually don't call plt.show() explicitly, because it's more convenient to just end the cell with the plot object.

I'm not sure if this will help with debugging or make things more confusing, but I also noticed that if you call plt.plot() before making your first uxarray plot, then plt.show() remains unnecessary. For example:

import matplotlib.pyplot as plt
import uxarray as ux

plt.plot([1,2,3,2,1])

uxds = ux.tutorial.open_dataset("outCSne30-vortex")
uxds["psi"].plot(backend="matplotlib")

plt.figure()  # this line isn't actually necessary but it doesn't hurt.
plt.plot([0, 1], [0, 1])

This will show two figures, first the [1,2,3,2,1] plot, then the second plot. All subsequent calls to plt.plot() do not seem to require plt.show(), which is good.

@rajeeja

rajeeja commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

@Sevans711 The no-plt.show() case comes from the same root as the earlier ones. hv.extension("matplotlib") doesn't just change the active Matplotlib backend — in a Jupyter kernel it also tears down IPython's display integration, and there's no public API to restore it piece by piece. The earlier attempts (restore the backend name, then re-register the inline hook) each reversed one symptom of that global side effect while leaving the next one broken; the last-line figure auto-display was the piece still missing. The fix now up takes a different approach: inside IPython it re-runs the shell's own backend activation (shell.enable_matplotlib(...), the public equivalent of the %matplotlib magic), which rebuilds the entire integration in one call, and falls back to mpl.use outside IPython. Verified in a real kernel across every case raised here — no-show() last-line, subsequent cells, explicit plt.show(), plot-before-uxarray, and the uxarray plot itself — all display correctly.

That said, this is still a patch over an upstream problem: the real bug is that hv.extension mutates global IPython/Matplotlib display state with no clean way to undo it. Filed upstream as holoviz/holoviews#6955. The alternative is to close this PR, document the plt.show() / %matplotlib inline workaround, and wait for the upstream fix. Either path is fine — this version makes the default Jupyter flow behave, but if relying on the shell's activation internals isn't worth it, closing and deferring to the upstream fix is the cleaner call.

@Sevans711 Sevans711 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, and I confirmed it works in the cases we discussed. Thank you for working through the IPython / matplotlib / hvplot / jupyter notebook internals to make this fix! Even though this relies on some of those internals, I suspect jupyter notebook plotting with matplotlib backend is a relatively common use-case, so I think it makes sense to merge to main.

The regression tests can serve as a gate for when the upstream fix is sufficient to avoid poking at those internals directly in uxarray. Maybe when the upstream fix is merged we could revisit this. The comments in the test docstrings probably serve as a sufficient reminder. In the meantime, I think it would be safe to merge and mark the issue as closed!

@rajeeja rajeeja merged commit 2be3c1f into main Jul 14, 2026
13 checks passed
@rajeeja rajeeja deleted the rajeeja/fix_matplotlib_backend branch July 14, 2026 16:17
@github-project-automation github-project-automation Bot moved this from 👀 In review to ✅ Done in UXarray Development Jul 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-benchmark Run ASV benchmark workflow

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

simple uxarray.plot(backend="matplotlib") seems to break subsequent xarray.plot functionality

5 participants